﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace CommonLibrary
{
    /// <summary>
    /// 显示运行信息代理
    /// </summary>
    /// <param name="isValid"></param>
    /// <param name="message"></param>
    public delegate void ShowRunningLogDelegate(bool isValid, string message);
    /// <summary>
    /// 插件配置参数
    /// </summary>
    public class PluginParamEntity
    {
        /// <summary>
        /// 名称
        /// </summary>
        [JsonIgnore]
        public string Tag { get; set; }
        /// <summary>
        /// 方法标记
        /// </summary>
        [JsonProperty(PropertyName = "Identity")]
        public string Identity { get; set; }
        /// <summary>
        /// 类名
        /// </summary>
        [JsonProperty(PropertyName = "ClassName")]
        public string ClassName { get; set; }
        /// <summary>
        /// 参数
        /// </summary>
        [JsonIgnore]
        public string ParamString { get; set; }
        /// <summary>
        /// 获取插件参数对象
        /// </summary>
        /// <param name="tag">使用场景</param>
        /// <param name="paramString">参数</param>
        /// <returns></returns>
        public PluginParamEntity Transfer(string tag, string paramString = "")
        {
            return new PluginParamEntity()
            {
                Tag = tag,
                Identity = this.Identity,
                ClassName = this.ClassName,
                ParamString = paramString
            };
        }
    }
    /// <summary>
    /// 插件定义信息
    /// </summary>
    public class PluginParam
    {
        /// <summary>
        /// 显示运行信息代理事件
        /// </summary>
        public static ShowRunningLogDelegate ShowRunningLog { get; set; } = null;
        /// <summary>
        /// 配置文件
        /// </summary>
        static string CONFIG_FILE_PATH = Path.Combine(CommonHelper.COMMON_CONFIG_DIR, "PluginParam.json");
        /// <summary>
        /// 插件信息
        /// </summary>
        List<PluginParamEntity> pluginParamEntities = new List<PluginParamEntity>();
        /// <summary>
        /// 锁
        /// </summary>
        private static object LOCK_OBJ = new object();
        /// <summary>
        /// 实例对象
        /// </summary>
        private static PluginParam instance = null;


        /// <summary>
        /// 实例对象
        /// </summary>
        /// <param name="reload"></param>
        /// <returns></returns>
        public static PluginParam Instance(bool reload = false)
        {
            if (reload || instance == null)
            {
                try
                {
                    instance = new PluginParam();
                    instance.pluginParamEntities = CommonHelper.GetJsonListFromFile<PluginParamEntity>(CONFIG_FILE_PATH);
                }
                catch (Exception ex)
                {
                    instance = new PluginParam();
                    LogHelper.Error(ex);
                }
            }
            return instance;
        }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            lock (LOCK_OBJ)
            {
                CommonHelper.SaveToJsonFile(instance.pluginParamEntities, CONFIG_FILE_PATH);
            }
        }
        /// <summary>
        /// 转换插件参数对象
        /// </summary>
        /// <param name="tag">使用场景</param>
        /// <param name="identity">标识</param>
        /// <param name="paramString">参数</param>
        /// <returns></returns>
        public PluginParamEntity GetPluginParam(string tag, string identity, string paramString = "")
        {
            PluginParamEntity result = null;
            if (!string.IsNullOrWhiteSpace(identity))
            {
                result = this.pluginParamEntities.Find(v => v.Identity.ToUpper().Equals(identity.ToUpper()));
                if (result != null)
                {
                    result = result.Transfer(tag, paramString);
                }
            }
            if (result == null)
            {
                LogHelper.Warn($"没有合适的方法，TAG：{tag}，IDENTITY：{identity}，PARAMSTRING：{paramString}");
            }
            return result;
        }
    }
}
